package com.test.util;

import net.coobird.thumbnailator.Thumbnails;

import javax.persistence.criteria.CriteriaBuilder;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 绍展
 * @Date
 **/
public class ColorUtil {
    //颜色标准数
    public static float colorNum=51.0F;
    //颜色标准
    public static Map<Integer,String> colorStandard;
    static {
        colorStandard=new HashMap<Integer, String>();
        colorStandard.put(0,"00");
        colorStandard.put(1,"33");
        colorStandard.put(2,"66");
        colorStandard.put(3,"99");
        colorStandard.put(4,"cc");
        colorStandard.put(5,"ff");
    }

    //十进制转十六进制
    public static String tenToHex(int x){
        Integer ten=new Integer(x);
        String hex=ten.toHexString(ten);
        return hex;
    }
    //十六进制转十进制
    public static Integer hexToTen(String hex){
        Integer x=Integer.parseInt(hex,16);
        return x;
    }
    public static String hexSum(String x,String y){
        Integer a=hexToTen(x);
        Integer b=hexToTen(y);
        return  tenToHex(new Integer(a+b));
    }

    public static String changeRgb(int r,int g,int b){
        //通过颜色标准数计算颜色标准key
        int rk=Math.round(Float.parseFloat(r+"")/colorNum);
        int gk=Math.round(Float.parseFloat(g+"")/colorNum);
        int bk=Math.round(Float.parseFloat(b+"")/colorNum);
        //通过颜色标准key取出标准颜色
        String rs=colorStandard.get(rk);
        String gs=colorStandard.get(gk);
        String bs=colorStandard.get(bk);
        return rs+gs+bs;
    }

    public static BufferedImage filter(BufferedImage src,float contrast,float brightness) throws IOException {
        int width = src.getWidth();
        int height = src.getHeight();

        BufferedImage dest=Thumbnails.of(src).scale(1).asBufferedImage();

        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        src.getRGB( 0, 0, width, height, inPixels, 0, width );

        // calculate RED, GREEN, BLUE means of pixel
        int index = 0;
        int[] rgbmeans = new int[3];
        double redSum = 0, greenSum = 0, blueSum = 0;
        double total = height * width;
        for(int row=0; row<height; row++) {
            int ta = 0, tr = 0, tg = 0, tb = 0;
            for(int col=0; col<width; col++) {
                index = row * width + col;
                ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                redSum += tr;
                greenSum += tg;
                blueSum +=tb;
            }
        }

        rgbmeans[0] = (int)(redSum / total);
        rgbmeans[1] = (int)(greenSum / total);
        rgbmeans[2] = (int)(blueSum / total);

        // adjust contrast and brightness algorithm, here
        for(int row=0; row<height; row++) {
            int ta = 0, tr = 0, tg = 0, tb = 0;
            for(int col=0; col<width; col++) {
                index = row * width + col;
                ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;

                // remove means
                tr -=rgbmeans[0];
                tg -=rgbmeans[1];
                tb -=rgbmeans[2];

                // adjust contrast now !!!
                tr = (int)(tr * contrast);
                tg = (int)(tg * contrast);
                tb = (int)(tb * contrast);

                // adjust brightness
                tr += (int)(rgbmeans[0] * brightness);
                tg += (int)(rgbmeans[1] * brightness);
                tb += (int)(rgbmeans[2] * brightness);

                int pixel=(ta << 24) | (clamp(tr) << 16) | (clamp(tg) << 8) | clamp(tb);
                outPixels[index] = pixel;
//                dest.setRGB(row,col,pixel);
            }
        }
        dest.setRGB(0,0,width,height,outPixels,0,width);
//        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
    }

    public static int clamp(int value) {
        return value > 255 ? 255 :(value < 0 ? 0 : value);
    }

    public static void main(String[] args) throws IOException {
//        BufferedImage bi=Thumbnails.of("F:\\work\\color\\20180615181145.jpg").scale(1).asBufferedImage();
//        bi=filter(bi,0.8f,1.0f);
//        Thumbnails.of(bi).scale(1).toFile(new File("F:\\work\\color\\test.jpg"));
        //90,74,63,-10859969
        System.out.println(tenToHex(10859969));
        System.out.println(tenToHex(90)+","+tenToHex(74)+","+tenToHex(63));
    }
}
